# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: team/alpine-desktop <bribbers@disroot.org>
pkgname=pipewire
pkgver=1.0.5
pkgrel=0
_so_ver=0.3
pkgdesc="Multimedia processing graphs"
url="https://pipewire.org/"
arch="all"
license="LGPL-2.1-or-later"
pkggroups="pipewire"
makedepends="
	alsa-lib-dev
	avahi-dev
	bash
	bluez-dev
	dbus-dev
	doxygen
	eudev-dev
	fdk-aac-dev
	glib-dev
	graphviz
	gst-plugins-base-dev
	gstreamer-dev
	jack-dev
	libcamera-dev
	libfreeaptx-dev
	liblc3-dev
	libusb-dev
	libx11-dev
	lilv-dev
	meson
	ncurses-dev
	pulseaudio-dev
	py3-docutils
	readline-dev
	roc-toolkit-dev
	sbc-dev
	vulkan-loader-dev
	xmltoman
	"
# move regular -dev after -jackdev, to steal files
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-alsa
	$pkgname-pulse
	$pkgname-jack
	$pkgname-jack-dev:jackdev
	$pkgname-dev
	gst-plugin-pipewire:gst_plugin
	$pkgname-zeroconf
	$pkgname-spa-bluez
	$pkgname-spa-vulkan
	$pkgname-tools
	$pkgname-spa-tools:spa_tools
	$pkgname-lang
	"
install="$pkgname.post-upgrade $pkgname.post-install"
source="https://gitlab.freedesktop.org/PipeWire/pipewire/-/archive/$pkgver/pipewire-$pkgver.tar.gz
	pipewire.desktop
	pipewire-launcher.sh
	"

case "$CARCH" in
	s390x)
		# libldac not available for big endian
		;;
	*)
		makedepends="$makedepends libldac-dev"
		;;
esac

case "$CARCH" in
	ppc64le|s390x|riscv64)
		# no webrtc-audio-processing
		;;
	*)
		makedepends="$makedepends webrtc-audio-processing-1-dev"
		subpackages="$subpackages $pkgname-echo-cancel:echo_cancel"
		;;
esac

# put libs last to steal residual files taken by some prior extras
subpackages="$subpackages $pkgname-libs"

build() {
	CFLAGS="$CFLAGS -O2" \
	CXXFLAGS="$CXXFLAGS -O2" \
	CPPFLAGS="$CPPFLAGS -O2" \
	abuild-meson \
		-Db_lto=true \
		-Dlibjack-path=/usr/lib \
		-Dlibv4l2-path=/usr/lib \
		-Ddocs=disabled \
		-Dman=enabled \
		-Dgstreamer=enabled \
		-Dexamples=enabled \
		-Dffmpeg=disabled \
		-Djack-devel=true \
		-Dsystemd=disabled \
		-Dvulkan=enabled \
		-Dsdl2=disabled \
		-Dlv2=enabled \
		-Dlibcamera=enabled \
		-Droc=enabled \
		-Dbluez5-codec-lc3=enabled \
		-Dsession-managers=[] \
		. output
	meson compile -C output
}

check() {
	meson test --no-rebuild --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output

	install -Dm644 "$srcdir"/pipewire.desktop -t "$pkgdir"/etc/xdg/autostart/
	install -Dm755 "$srcdir"/pipewire-launcher.sh "$pkgdir"/usr/libexec/pipewire-launcher
}

alsa() {
	pkgdesc="ALSA support for pipewire"
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/alsa-lib
	amove usr/share/alsa/alsa.conf.d

	install -d "$subpkgdir"/etc/alsa/conf.d
	for i in "$subpkgdir"/usr/share/alsa/alsa.conf.d/*.conf; do
		ln -s -- "${i#"$subpkgdir"}" "$subpkgdir"/etc/alsa/conf.d/
	done
}

pulse() {
	pkgdesc="Pulseaudio support for pipewire"
	depends="
		pipewire-session-manager
		pulseaudio-utils
		"
	provides="pulseaudio=$pkgver-r$pkgrel pulseaudio-bluez=$pkgver-r$pkgrel"
	provider_priority=1

	amove usr/bin/pipewire-pulse
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-protocol-pulse.so
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-pulse-tunnel.so
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-filter-chain-lv2.so
	amove usr/share/pipewire/pipewire-pulse.conf
	amove usr/share/pipewire/pipewire-pulse.conf.avail

}

jack() {
	pkgdesc="JACK support for pipewire"
	depends="pipewire-session-manager"
	provides="jack=$pkgver-r$pkgrel"
	replaces="jack"

	amove usr/lib/libjack*.so.*
	amove usr/bin/pw-jack
	amove usr/lib/spa-*/jack/libspa-jack.so
	amove usr/share/pipewire/jack.conf
}

gst_plugin() {
	pkgdesc="Multimedia graph framework - PipeWire plugin"
	depends="pipewire-session-manager gst-plugins-base"

	amove usr/lib/gstreamer-1.0
}

echo_cancel() {
	pkgdesc="WebRTC-based echo canceller module for PipeWire"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/pipewire-$_so_ver/libpipewire-module-echo-cancel.so
}

zeroconf() {
	pkgdesc="$pkgdesc - Zeroconf support"
	depends=""
	provides="pulseaudio-zeroconf=$pkgver-r$pkgrel"
	provider_priority=1

	amove usr/lib/pipewire-$_so_ver/libpipewire-module-zeroconf-discover.so
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-raop-*.so
}

bluez() {
	pkgdesc="PipeWire BlueZ5 SPA plugin (Bluetooth)"
	depends=""
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/spa-*/bluez5
}

vulkan() {
	pkgdesc="PipeWire Vulkan SPA plugin"
	depends=""

	amove usr/lib/spa-*/vulkan
}

tools() {
	pkgdesc="PipeWire tools"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/pw-*
}

spa_tools() {
	pkgdesc="PipeWire SPA tools"
	depends=""
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/spa-*
}

jackdev() {
	pkgdesc="JACK development files (from pipewire)"
	provides="jack-dev=$pkgver-r$pkgrel"

	amove usr/lib/libjack*.so
	amove usr/include/jack
	amove usr/lib/pkgconfig/jack.pc
}

libs() {
	default_libs

	# the libraries must be paired with the client config and the plugins,
	# see:
	# https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3296
	# https://bugs.archlinux.org/task/78813
	amove \
		usr/share/pipewire/client*.conf* \
		usr/lib/pipewire-*/ \
		usr/lib/spa-*/
}

sha512sums="
ed561154b2b5182c94142e03b1147a90a98e788cb5f455a5202ee8c79c1f41e19c2235b3e06ca7af68f2c76320f7c2f8c71db147c9f15f82bef50fccb8ef985f  pipewire-1.0.5.tar.gz
d5d8bc64e42715aa94296e3e26e740142bff7f638c7eb4fecc0301e46d55636d889bdc0c0399c1eb523271b20f7c48cc03f6ce3c072e0e8576c821ed1ea0e3dd  pipewire.desktop
e46939b8f903fe6b7421cd42d0746e669402d76afe3326401c186fefeb725e3c126a00ba9f315067d2535991134a24afd855752d757e9e52c20191b5d388f99b  pipewire-launcher.sh
"
